package org.benjamin.lending.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.BorrowForm;
import org.benjamin.lending.domain.entity.BorrowInfo;
import org.benjamin.lending.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by xywen on 2017/5/31.
 */
@Controller
@RequestMapping("borrow")
public class BorrowController {

    private final Logger logger = LogManager.getLogger();

    @Autowired
    private BorrowService borrowService;

    @RequestMapping("index")
    public String index(BorrowForm borrowForm, ModelMap modelMap){
        modelMap.addAttribute("borrowForm",borrowForm);
        Page<BorrowInfo> borrowInfos = this.borrowService.findBorrowInfoPage();
        logger.info("查询借款表分页信息：{}",borrowInfos.getTotalPages());
        modelMap.addAttribute("borrowInfos",borrowInfos);
        return "borrow.index";
    }

    @RequestMapping("detail/id/{id}")
    public String detail(@PathVariable("id") Integer id,ModelMap modelMap){
        BorrowInfo borrowInfo = this.borrowService.getBorrowById(id);
        modelMap.addAttribute("borrowInfo",borrowInfo);
        return "borrow.detail";
    }

    @RequestMapping("borrowList")
    public String borrowList(ModelMap modelMap){
        Page<BorrowInfo> borrowInfos = this.borrowService.findBorrowInfoPage();
        logger.info("查询借款表分页信息：{}",borrowInfos.getTotalPages());
        modelMap.addAttribute("borrowInfos",borrowInfos);
        return "borrow/borrowList";
    }
}
